//
//  HJPopMenuController.m
//  KaiChat
//
//  Created by lx on 2023/10/25.
//

#import "HJPopMenuController.h"
#import "HJButton.h"


@interface HJPopMenuData : NSObject

@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, assign) HJPopMenuType type;
+ (HJPopMenuData *)dataWithBlock:(void (^)(HJPopMenuData *model))block;

@end


@implementation HJPopMenuData

+ (HJPopMenuData *)dataWithBlock:(void (^)(HJPopMenuData *model))block {
    HJPopMenuData *data = [[HJPopMenuData alloc] init];
    if (block) block(data);
    return data;
}


@end


@interface HJPopMenuController ()

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, strong) NSDictionary *typeDict;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation HJPopMenuController
 

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.offsetY = 0;
    }
    return self;
}


- (void)showInController:(UIViewController *)controller {
    [controller presentViewController:self animated:NO completion:nil];
}
 
- (HJPopMenuController *)initWithTypes:(NSArray *)types {
    if (self = [super init]) {
        for (id  _Nonnull obj in types) {
            [self.datas addObject:[self.typeDict objectForKey:obj]];
        }
    }
    return self;
 
}

 
- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    _maskView = maskView;
    [maskView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];

    CGFloat contentW = HZoom(146);
    CGFloat contentH = HZoom(40) * self.datas.count;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.shadowColor = UIColor.blackColor.CGColor;
    contentView.layer.shadowOffset = CGSizeZero;
    contentView.layer.shadowRadius = 20;
    contentView.layer.shadowOpacity = 0.1;
    contentView.layer.cornerRadius = 10;

     _contentView = contentView;
    
    if ([_controllerName isEqualToString:@"HJMessageController"]) {
        
        if (CGRectGetMaxX(self.sourceRect) + 80 > KScreenWidth) {//消息在右边, 已右边的对其
            contentView.frame = CGRectMake(CGRectGetMaxX(self.sourceRect) - contentW, CGRectGetMaxY(self.sourceRect) + 10, contentW, contentH);
        } else {//消息在左边
            contentView.frame = CGRectMake(CGRectGetMinX(self.sourceRect), CGRectGetMaxY(self.sourceRect) + 10, contentW, contentH);
        }
        
        if (CGRectGetMaxY(contentView.frame) > KScreenHeight - TabBarHeight) {
            contentView.tj_y = CGRectGetMinY(self.sourceRect) - 10 - contentH;
        }
    } else if ([_controllerName isEqualToString:@"HJConversationController"]) {
        contentW = HZoom(150);
        contentView.frame = CGRectMake(KScreenWidth - contentW - 20, KNavBarHight, contentW, contentH);
    } else {
        
        contentW = HZoom(170);
        contentView.frame = CGRectMake(self.view.tj_width - contentW - leftRightMargin, self.sourceRect.origin.y + self.sourceRect.size.height, contentW, contentH);
        if (CGRectGetMaxY(contentView.frame) > KScreenHeight - TabBarHeight) {
            contentView.frame = CGRectMake(self.view.tj_width - contentW - leftRightMargin, self.sourceRect.origin.y - contentH, contentW, contentH);
        }
        
    }
     
    UIView *fileContentView = [UIView addToView:contentView];
    [fileContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(40 * self.datas.count);
    }];
    
    CGFloat H = HZoom(40);
    [self.datas enumerateObjectsUsingBlock:^(HJPopMenuData *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        HJButton *itemButton = [HJButton buttonWithSuperView:fileContentView font:UIFontMake(14) color:obj.type == HJPopMenuTypeDelete ? UIColor.tj_FF5100Color : UIColor.tj_333333Color title:obj.text];
        [itemButton setImage:[UIImage imageNamed:obj.icon] forState:UIControlStateNormal];
        itemButton.anyObject = @(obj.type);
        itemButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        itemButton.imagePosition = QMUIButtonImagePositionLeft;
        itemButton.spacingBetweenImageAndTitle = 10;
        [itemButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [itemButton addTarget:self action:@selector(didClickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        [itemButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(H * idx);
            make.width.offset(KScreenWidth);
            make.height.offset(H);
        }];
    }];

}

- (void)didClickItemButton:(HJButton *)button {
    [self dismissComplete:^{
        if (self.didSelectItemBlock) {
            self.didSelectItemBlock([button.anyObject intValue]);
        }
    }];
}
 
- (void)dismiss {
    [self dismissComplete:nil];
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)dismissComplete:(void (^)(void))complete {

    [self dismissViewControllerAnimated:NO completion:^{
        if (complete) {
            complete();
        }
        
        if (self.dismissBlock) {
            self.dismissBlock();
        }
    }];
}

 
- (NSDictionary *)typeDict {
    if (!_typeDict) {
        _typeDict = @{
            @(HJPopMenuTypeCopy): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Copy";
                model.icon = @"popMenuCopy";
                model.type = HJPopMenuTypeCopy;
            }],
            @(HJPopMenuTypeDelete): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Delete";
                model.icon = @"popMenuDelete";
                model.type = HJPopMenuTypeDelete;
            }],
            @(HJPopMenuTypeMute): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Mute";
                model.icon = @"popMenuMute";
                model.type = HJPopMenuTypeMute;
            }],
            @(HJPopMenuTypeUnmute): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Unmute";
                model.icon = @"popMenuUnMute";
                model.type = HJPopMenuTypeUnmute;
            }],
            @(HJPopMenuTypeTop): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Top";
                model.icon = @"popMenuTop";
                model.type = HJPopMenuTypeTop;
            }],
            @(HJPopMenuTypeCancelTop): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Cancel Top";
                model.icon = @"popMenuCancelTop";
                model.type = HJPopMenuTypeCancelTop;
            }],
            @(HJPopMenuTypeAddFriend): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Add Friends";
                model.icon = @"addContact";
                model.type = HJPopMenuTypeAddFriend;
            }],
            @(HJPopMenuTypeCreateGroup): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Create Group";
                model.icon = @"createGroup";
                model.type = HJPopMenuTypeCreateGroup;
            }],
            @(HJPopMenuTypeNewsFollow): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Follow";
                model.icon = @"news_unFollow_icon";
                model.type = HJPopMenuTypeNewsFollow;
            }],
            @(HJPopMenuTypeNewsUnFollow): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Unsubscribe";
                model.icon = @"news_follow_icon";
                model.type = HJPopMenuTypeNewsUnFollow;
            }],
            @(HJPopMenuTypeNewsDelete): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Delete";
                model.icon = @"news_delete_icon";
                model.type = HJPopMenuTypeNewsDelete;
            }],
            @(HJPopMenuTypeNewsHiden): [HJPopMenuData dataWithBlock:^(HJPopMenuData *model) {
                model.text = @"Hide this module";
                model.icon = @"news_hide_icon";
                model.type = HJPopMenuTypeNewsHiden;
            }],
        };
    }
    return _typeDict;
}

@end
